import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/Layout'

Vue.use(Router)
// 固定路由
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index')
  },
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [{
      meta: {
        title: '首页'
      },
      path: 'home',
      name: 'Home',
      component: () => import('@/views/home/index'),
    }]
  },
  // {
  //   path: '/system',
  //   component: Layout,
  //   name: 'system',
  
  //   children: [
  //     {
  //       path: '/system/user',
  //       name: 'user',
  //       component: () => import('@/views/system/user'),
  //     },
  //     {
  //       path: '/system/role',
  //       name: 'role',
  //       component: () => import('@/views/system/role'),
  //     },
  //     {
  //       path: '/system/menu',
  //       name: 'menu',
  //       component: () => import('@/views/system/menu'),
  //     },
  //   ]
  // },
  // {
  //   path: '/log',
  //   component: Layout,
  //   name: 'log',
  //   children: [
  //     {
  //       path: '/log/action',
  //       name: 'action',
  //       component: () => import('@/views/log/action'),
  //     },
  //   ]
  // },
  // {
  //   path: '/app-vue',
  //   component: Layout,
  //   name: 'appVue',
  //   children: [
  //     {
  //       path: '/app-vue/reward',
  //       name: 'reward',
  //       children: [
  //         {
  //           path: '/app-vue/reward/list',
  //           name: 'rewardList'
  //         },
  //         {
  //           path: '/app-vue/reward/package',
  //           name: 'rewardPackage'
  //         }
  //       ]
  //     }
  //   ]
  // },
  
//   {
//     path: '/auth-redirect',
//     component: () => import('@/views/login/authredirect'),
//     hidden: true
//   },
//   {
//     path: '/portal-error401',
//     hidden: true,
//     name: '401',
//     meta: { title: '无权限' },
//     component: () => import('@/views/errorPage/401')
//   },
//   {
//     path: '/portal-error404',
//     hidden: true,
//     name: '404',
//     meta: { title: '找不到该页面' },
//     component: () => import('@/views/errorPage/404')
//   }
]

// 创建 路由 对象
const createRouter = () =>
  new Router({
    mode: 'history',
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  })
  
const router = createRouter()

const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

// 动态路由更新 see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router